package com.kanshu.ksgb.fastread.doudou.common.business.manager;

import android.util.LruCache;

import com.kanshu.ksgb.fastread.doudou.app.constants.Constants;
import com.kanshu.ksgb.fastread.doudou.common.business.utils.UserUtils;
import com.qmuiteam.qmui.util.QMUINotchHelper;
import com.tencent.mmkv.MMKV;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * mmkv管理器（和用户数据无关的存储在这儿）
 * 作者    wcy
 * 时间    2018/11/16 10:04
 * 文件    doudou
 * 描述
 */
public class MMKVDefaultManager {
    private volatile static MMKVDefaultManager sManager;
    // 默认的mmkv实例
    private MMKV mMMKV;
    // 关于书籍的mmkv缓存
    private LruCache<String, MMKV> mMMKVCache;
    // 书籍阅读器mmkv前缀
    public static String BOOK_READER_PREFIX = "book_reader_";
    // 用户书籍阅读器阅读进度前缀
    public static String USER_BOOK_PREFIX = "#user_";

    private MMKVDefaultManager() {
        mMMKV = MMKV.defaultMMKV();
        mMMKVCache = new LruCache<>(8);
    }

    public static MMKVDefaultManager getInstance() {
        if (sManager == null) {
            synchronized (MMKVDefaultManager.class) {
                if (sManager == null) {
                    sManager = new MMKVDefaultManager();
                }
            }
        }
        return sManager;
    }

    /**
     * 是否需要全量更新章节列表
     */
    public boolean isNeedUpdateChapterList(String book_id) {
        long time = mMMKV.getLong("all_simple_chapters@" + book_id, 0L);
        return System.currentTimeMillis() - time > 4 * 60 * 60 * 1000;
    }

    /**
     * 保存上一次更新的时间戳
     */
    public void setUpdateChapterListTime(String book_id) {
        mMMKV.edit().putLong("all_simple_chapters@" + book_id, System.currentTimeMillis()).apply();
    }

    /**
     * 保存用户设备绑定的UserID
     */
    public void setDeviceUserId(String deviceUserId) {
        mMMKV.putString("device_userid", deviceUserId).commit();
    }

    /**
     * 获取用户设备绑定的UserID
     */
    public String getDeviceUserId() {
        return mMMKV.getString("device_userid", UserUtils.getUserId());
    }

    /**
     * 保存用户掉线的UserID
     */
    public void setOfflineUserId(String offlineUserId) {
        mMMKV.putString("offline_userid", offlineUserId).commit();
    }

    /**
     * 获取用户掉线的UserID
     */
    public String getOfflineUserId() {
        return mMMKV.getString("offline_userid", "");
    }

    /**
     * 保存书籍分类和连载
     *
     * @param writing_process 连载或者完结
     * @param category_id_1   书籍分类
     */
    public void setBookAttribute(String book_id, String writing_process, String category_id_1) {
        mMMKV.edit().putString("@book_writing_process" + book_id, writing_process + "#" + category_id_1).apply();
    }

    public String getBookAttribute(String book_id) {
        return mMMKV.getString("@book_writing_process" + book_id, null);
    }

    /**
     * 获取每一本书所属的mmkv
     *
     * @param bookId
     * @return
     */
    public MMKV getBookMMKV(String bookId) {
        String key = BOOK_READER_PREFIX + bookId;
        MMKV mmkv = mMMKVCache.get(key);
        if (mmkv != null) {
            return mmkv;
        }
        mmkv = MMKV.mmkvWithID(key);
        mMMKVCache.put(key, mmkv);
        return mmkv;
    }

    /**
     * 获取用户书籍阅读进度MMKV
     *
     * @return
     */
    public MMKV getUserMMKV() {
        String key = USER_BOOK_PREFIX + UserUtils.getUserId();
        MMKV mmkv = mMMKVCache.get(key);
        if (mmkv != null) {
            return mmkv;
        }
        mmkv = MMKV.mmkvWithID(key);
        mMMKVCache.put(key, mmkv);
        return mmkv;
    }

    public MMKV getDefaultMMKV() {
        return mMMKV;
    }

    public void saveEveryDayDialogTime(long time) {
        mMMKV.edit().putLong("every_day_dialog_time", time).apply();
    }

    /**
     * 显示每天对话框
     *
     * @return
     */
    public boolean needShowEveryDayDialog() {
        boolean needShow;
        long lastTime = mMMKV.getLong("every_day_dialog_time", 0);
        long nowTime = System.currentTimeMillis();
        if (lastTime == 0) {
            needShow = true;
        } else {
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
            Date now = new Date(nowTime);
            Date last = new Date(lastTime);
            needShow = !dateFormat.format(now).equals(dateFormat.format(last));
        }
        if (needShow) {
            saveEveryDayDialogTime(nowTime);
        }
        return needShow;
    }

    public String getPayInfo() {
        return mMMKV.getString(Constants.PAY_INFO, "");
    }

    public void savePayInfo(String payInfo) {
        mMMKV.edit().putString(Constants.PAY_INFO, payInfo).apply();
    }

    public int isFreeVersion() {
        return mMMKV.getInt("is_free_version", 0);
    }

    public void setIsFreeVersion(int isFreeVersion) {
        mMMKV.edit().putInt("is_free_version", isFreeVersion).apply();
    }

    public void setDailyCoinReward(int count) {
        mMMKV.edit().putInt("daily_coin_reward", count).apply();
    }

    public int getDailyCoinReward() {
        return mMMKV.getInt("daily_coin_reward", 1);
    }

    /**
     * 获取赚钱开关
     *
     * @return
     */
    public boolean isEnableMakeMoneyModule() {
        return mMMKV.getBoolean("enable_make_money", true);
    }

    public void setEnableMakeMoneyModule(boolean enableMakeMoneyModule) {
        mMMKV.edit().putBoolean("enable_make_money", enableMakeMoneyModule).apply();
    }

    /**
     * 客服qq
     *
     * @return
     */
    public String getCustomerServiceQQ() {
        return mMMKV.getString("csqq", "");
    }

    /**
     * @param qq
     */
    public void setCustomerServiceQQ(String qq) {
        mMMKV.edit().putString("csqq", qq).apply();
    }

    /**
     * 获取补丁的版本
     *
     * @return
     */
    public int getPatchVersion() {
        return mMMKV.getInt("patch_version", 0);
    }

    /**
     * 设置补丁的版本
     *
     * @param version
     */
    public void setPatchVersion(int version) {
        mMMKV.edit().putInt("patch_version", version).apply();
    }


    /**
     * 保存服务器设置的超时时间
     */
    public void saveServerReadTime(String time) {
        mMMKV.edit().putString("server_read_time", time).apply();
    }

    public String getServerReadTime() {
        return mMMKV.getString("server_read_time", "");
    }


    private boolean mIsClearCache;

    public boolean isClearCache() {
        return mIsClearCache;
    }

    public void setClearCache(boolean isClearCache) {
        mIsClearCache = isClearCache;
    }


    private boolean mHttpLogSwitch;

    private int mHttpTimeoutUpload;

    public boolean isHttpLogSwitch() {
        return mHttpLogSwitch;
    }

    public void setHttpLogSwitch(boolean httpLogSwitch) {
        mHttpLogSwitch = httpLogSwitch;
    }

    public void setHttpTimeoutUpload(int httpTimeoutUpload) {
        mHttpTimeoutUpload = httpTimeoutUpload;
    }

    public int getHttpTimeoutUpload() {
        if (mHttpTimeoutUpload == 0) {
            mHttpTimeoutUpload = 4;
        }
        return mHttpTimeoutUpload;
    }

    /**
     * 获取刘海高度
     *
     * @return
     */
    public int getNotchHeight() {
        if (QMUINotchHelper.isNotchOfficialSupport()) {
            return mMMKV.getInt("notch_height", 0);
        } else {
            return 0;
        }
    }

    /**
     * 保存刘海高度
     *
     * @param height
     */
    public void setNotchHeight(int height) {
        mMMKV.edit().putInt("notch_height", height).apply();
    }

    /**
     * 客服qq
     *
     * @return
     */
    public boolean isFirstShowLuoMi() {
        return mMMKV.getBoolean("is_first_show_luomi", true);
    }

    /**
     * @param b
     */
    public void setFirstShowLuoMi(boolean b) {
        mMMKV.edit().putBoolean("is_first_show_luomi", b).apply();
    }
}
